package com.example.spring_boot_demo.controller;

import com.example.spring_boot_demo.dto.ClazzRoom;
import com.example.spring_boot_demo.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <P> @Author: SongKeCheng
 * <P> Type: Class
 * <P> Description:
 * <P> Date: 2024/12/18:10:20
 */

@SuppressWarnings({"unchecked", ""})
@RequestMapping("/form")
@RestController
public class FormController {
    private static final Logger log = LoggerFactory.getLogger(FormController.class);



    //form/StringValue
    @RequestMapping("/StringValue")
    public String receiveParameters(@RequestParam(value = "name",required = false,defaultValue = "defaultValue") String userName){
        System.out.println(userName);
        //index
        return userName;
    }

    /**
     *接收前端form提交的数组
     * @param ids
     */
    @RequestMapping("/Arrays")
    @ResponseBody
    //preHandle
    public Map receiveParameters(int[] ids){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("code",200);
        return hashMap;
    }
    //postHandle
    //afterCompletion

    /**
     * 传入一个数组，后端自动封装成List集合
     * @param list
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map receiveParameters(@RequestParam List<Integer> list){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("list",list);
        hashMap.put("code",200);
        return hashMap;
    }

    void test(){
        HttpServletRequest request = null;
        User user1 = new User();
        String name = request.getParameter("name");
        if(name != null){
            user1.setName(name);
        }

        String age = request.getParameter("age");
        receiveParameters(user1);
    }
    /**
     * 传入一堆数据，后端自动封装 User 对象
     * @param user
     */
    @ResponseBody
    //@RequestMapping(value = "/user",method = RequestMethod.POST)
    @PostMapping("/user")
    //@GetMapping
    //@PutMapping
    //@DeleteMapping
    public Map receiveParameters(User user){

        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        return hashMap;
    }
    /**
     * 复合对象数据传输
     * @param clazzRoom
     */
    @ResponseBody
    @PostMapping("/ClazzUserValue")
    public Map clazzUserValue(ClazzRoom clazzRoom){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("clazzRoom",clazzRoom);
        hashMap.put("code",200);
        return hashMap;
    }

    /**
     * 嵌套对象 使用json传输
     * @param clazzRoom
     */
    @PostMapping("/ClazzUserValue2Json")
    @ResponseBody
    public Map clazzUserValue2Json(@RequestBody ClazzRoom clazzRoom){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("clazzRoom",clazzRoom);
        hashMap.put("code",200);
        hashMap.put("msg","使用json传输");
        return hashMap;
    }

    /**
     * 嵌套对象 使用json传输
     * @param user
     */
    @PostMapping("/userValue2Json")
    @ResponseBody
    public Map userValue2Json(@RequestBody User user){
        Map<Object, Object> hashMap = new HashMap<>();
        hashMap.put("user",user);
        hashMap.put("code",200);
        hashMap.put("msg","使用json传输");
        return hashMap;
    }

    @ResponseBody
    @GetMapping("/mutiParams")
    public String receiveParameters(String userName,
                                    String password){

        //index
        return userName + "-" + password;
    }
}
